//
//  YWMissionCancelCell.m
//  maintain
//
//  Created by kyo on 17/1/6.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWMissionCancelCell.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UILabel+AttributedText.h"
#import "YWCommonTool.h"
#import "YWReminder.h"
#import "YWDictMaintenanceType.h"

@interface YWMissionCancelCell ()
@property (nonatomic, strong) UIView *backGroundIV;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *contentLabel;

@property (nonatomic, strong) UILabel *missionTitleLabel;
@property (nonatomic, strong) UILabel *missionTypeLabel;
@property (nonatomic, strong) UILabel *missionCodeLabel;

@end

@implementation YWMissionCancelCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor customBackgroudColor];
        [self autolayoutViews];
    }
    return self;
}

- (void)setReminder:(YWReminder *)reminder{
    _reminder = reminder;
    self.dateLabel.text = [YWCommonTool showTime:reminder.createTime/1000 andFormatter:KTimeFormatShortTime];
    self.timeLabel.text = [YWCommonTool showTime:reminder.createTime/1000 andFormatter:KTimeFormatMonthDay];
    
    self.titleLabel.text = reminder.title?:NSLocalizedString(@"reminderTypeOfMissionCancel", nil);
    self.contentLabel.text = reminder.content?:NSLocalizedString(@"youHaveAMissionIsCanceled", nil);
    
    NSString *maintenanceType = [YWCommonTool maintenanceTypeOfMaintenanceTypeId:reminder.missionType];
    [self.missionTypeLabel labelText:NSLocalizedString(@"missionTypeFormatter", nil) withAttributedText:maintenanceType andAttributedColor:[UIColor blackColor]];
    [self.missionCodeLabel labelText:NSLocalizedString(@"missionSequenceFormatter", nil) withAttributedText:reminder.missionSequence?:NSLocalizedString(@"none", nil) andAttributedColor:[UIColor blackColor]];
    
    if (reminder.missionType == YWMaintenanceTypePatrol) {
        [self.missionTitleLabel labelText:NSLocalizedString(@"lineNameFormatter", nil) withAttributedText:reminder.missionTitle?:NSLocalizedString(@"none", nil) andAttributedColor:[UIColor blackColor]];
    }else if (reminder.missionType == YWMaintenanceTypeKeepWatch){
        [self.missionTitleLabel labelText:NSLocalizedString(@"lineNameFormatter", nil) withAttributedText:reminder.missionTitle?:NSLocalizedString(@"none", nil) andAttributedColor:[UIColor blackColor]];
    }else {
        [self.missionTitleLabel labelText:@"任务名称: " withAttributedText:reminder.missionTitle?:NSLocalizedString(@"none", nil) andAttributedColor:[UIColor blackColor]];
    }
    
    self.contentLabel.preferredMaxLayoutWidth = self.contentLabel.frame.size.width;
    self.missionTitleLabel.preferredMaxLayoutWidth = self.missionTitleLabel.frame.size.width;
}

- (void)autolayoutViews{
    UIView *superView = self.contentView;
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(superView.mas_top).offset(kTopPadding);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(kMinLabelH);
    }];
    [self.backGroundIV mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_dateLabel.mas_bottom).offset(kMarginW);
        make.left.mas_equalTo(superView.mas_left).offset(kMarginW);
        make.right.mas_equalTo(superView.mas_right).offset(-kMarginW);
        make.bottom.mas_equalTo(superView.mas_bottom).offset(-kMarginW);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_backGroundIV.mas_top).offset(8);
        make.left.mas_equalTo(_backGroundIV.mas_left).offset(kMarginW);
        make.right.mas_equalTo(_backGroundIV.mas_right).offset(-kMarginW);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_titleLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_titleLabel.mas_left);
        make.right.mas_equalTo(_titleLabel.mas_right);
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_timeLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_timeLabel.mas_left);
        make.right.mas_equalTo(_timeLabel.mas_right);
    }];
    [self.missionTitleLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_contentLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_contentLabel.mas_left);
        make.right.mas_equalTo(_contentLabel.mas_right);
    }];
    [self.missionTypeLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_missionTitleLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_contentLabel.mas_left);
        make.right.mas_equalTo(_contentLabel.mas_right);
    }];
    [self.missionCodeLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_missionTypeLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_contentLabel.mas_left);
        make.right.mas_equalTo(_contentLabel.mas_right);
        make.bottom.mas_equalTo(_backGroundIV.mas_bottom).offset(-8);
    }];
    
}

- (UILabel *)dateLabel{
    if (!_dateLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor customTextGrayColor];
        [self.contentView addSubview:label];
        _dateLabel = label;
    }
    return _dateLabel;
}

- (UIView *)backGroundIV{
    if (!_backGroundIV) {
        UIView *backGroundIV = [[UIView alloc] init];
        backGroundIV.clipsToBounds = YES;
        backGroundIV.layer.cornerRadius = 4.0;
        backGroundIV.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:backGroundIV];
        _backGroundIV = backGroundIV;
    }
    return _backGroundIV;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = KTitleFont;
        [self.backGroundIV addSubview:label];
        _titleLabel = label;
    }
    return _titleLabel;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _timeLabel = label;
    }
    return _timeLabel;
}

- (UILabel *)contentLabel{
    if (!_contentLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _contentLabel = label;
    }
    return _contentLabel;
}

- (UILabel *)missionTitleLabel{
    if (!_missionTitleLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _missionTitleLabel = label;
    }
    return _missionTitleLabel;
}

- (UILabel *)missionTypeLabel{
    if (!_missionTypeLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _missionTypeLabel = label;
    }
    return _missionTypeLabel;
}

- (UILabel *)missionCodeLabel{
    if (!_missionCodeLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _missionCodeLabel = label;
    }
    return _missionCodeLabel;
}

@end
